package cn.dyp.com.getsensordata.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import cn.dyp.com.getsensordata.R;
import cn.dyp.com.getsensordata.activity.MainActivity;
import cn.dyp.com.getsensordata.adapter.SensorGridAdapter;
import cn.dyp.com.getsensordata.bean.SensorBean;
import cn.dyp.com.getsensordata.bean.SensorConfig;
import cn.dyp.com.getsensordata.bean.SensorValue;
import cn.dyp.com.getsensordata.ClientApp;

/**
 * 显示环境指标界面的Fragment
 * 
 * @author asus
 */

public class EnvirFragment extends Fragment {
	public static final String TAG = "envir_fragment";
	private GridView mGridView;
	private SensorGridAdapter mAdpater;
	private List<SensorBean> mList;
	private ClientApp mApp;
	private String[] mStrings;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mStrings = getActivity().getResources().getStringArray(R.array.sensor);
		mApp = (ClientApp) getActivity().getApplication();
		iniData();
	}

	/**
	 * 初始化数据
	 */
	private void iniData() {
		mList = new ArrayList<SensorBean>();
		SensorBean mBean;
		Random mRandom = new Random();
		for (int i = 0; i < mStrings.length; i++) {
			mBean = new SensorBean("");
			mBean.setName(mStrings[i]);
			mBean.setValue(i + mRandom.nextInt(10));
			mBean.setMaxValue(i + mRandom.nextInt(3));
			mBean.setMinValue(mRandom.nextInt(5));
			mList.add(mBean);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.envir_fragment, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		iniView();
		setGridView();
		upDateView();
		MainActivity mActivity = (MainActivity) getActivity();
		mActivity.setUpDate(new MainActivity.OnUpDate() {

			@Override
			public void upDate() {
				upDateView();
			}
		});
	}

	/**
	 * GridView设置适配器
	 */
	private void setGridView() {
		mAdpater = new SensorGridAdapter(getActivity(), mList);
		mGridView.setAdapter(mAdpater);
	}

	/**
	 * 初始化Gridview
	 */
	private void iniView() {
		mGridView = (GridView) getView().findViewById(R.id.envir_gridview);
	}

	/**
	 * 更新数据
	 * 
	 */
	public void upDateView() {
		mList.clear();
		SensorBean mBean;
		SensorValue mValue = mApp.getCurSensorValue();
		SensorConfig mConfig = mApp.getSensorConfig();
		// 设置co2传感器的数据
		mBean = new SensorBean(mStrings[0]);
		mBean.setMaxValue(mConfig.maxCo2);
		mBean.setMinValue(mConfig.minCo2);
		mBean.setValue(mValue.getCo2());
		mList.add(mBean);
		// 设置光照传感器的数据
		mBean = new SensorBean(mStrings[1]);
		mBean.setMaxValue(mConfig.maxLight);
		mBean.setMinValue(mConfig.minLight);
		mBean.setValue(mValue.getLight());
		mList.add(mBean);
		// 设置空气温度传感器的数据
		mBean = new SensorBean(mStrings[2]);
		mBean.setMaxValue(mConfig.maxAirTemperature);
		mBean.setMinValue(mConfig.minAirTemperature);
		mBean.setValue(mValue.getAirTemper());
		mList.add(mBean);
		// 设置空气湿度传感器的数据
		mBean = new SensorBean(mStrings[3]);
		mBean.setMaxValue(mConfig.maxAirHumidity);
		mBean.setMinValue(mConfig.minAirHumidity);
		mBean.setValue(mValue.getAirHumid());
		mList.add(mBean);
		// 设置土壤温度传感器的数据
		mBean = new SensorBean(mStrings[4]);
		mBean.setMaxValue(mConfig.maxSoilTemperature);
		mBean.setMinValue(mConfig.minSoilTemperature);
		mBean.setValue(mValue.getSoilTemper());
		mList.add(mBean);
		// 设置土壤湿度传感器的数据
		mBean = new SensorBean(mStrings[5]);
		mBean.setMaxValue(mConfig.maxSoilHumidity);
		mBean.setMinValue(mConfig.minSoilHumidity);
		mBean.setValue(mValue.getSoilHumid());
		mList.add(mBean);

		mAdpater.notifyDataSetChanged();
	}
}
